#ifndef _CHUNK_H
#define _CHUNK_H

#include <stdlib.h>
#include "htree.h"
#include "buffer.h"
#include <unistd.h>

extern const size_t CACHE_BUFFER_COUNT;
extern const size_t CACHE_BUFFER_SIZE;

/**
 * wr_index can not beyond array len
 * rd_index can not beyond wr_index
 **/
struct cache{
	int buffer_size;
	int rd_index;
	int wr_index;
	struct buffer *data_arr[16];
	
};

struct store_chunk{
       char name[33];
       int rd_fd;
       int wr_fd;
       off_t wr_index;
       off_t rd_index;
	
	struct cache *cc_rd;
	struct cache *cc_wr;
	
	struct bufferFactory *bf_hdl;
	struct h_tree *hashtree;
};


extern int chunk_repair_rdfd(struct store_chunk *chunk);
extern int chunk_repair_wrfd(struct store_chunk *chunk);
/**
 * 一次写入整个buffer链表
 * return 0  正常写入。
 * return -1 写入文件出错，存储块写入偏移量回到上一个偏移处
 */
extern int write_data(struct store_chunk *chunk,struct buffer *buff);
/**
 * 一个数据单元采用buffer链表
 * return 0  正常读取，未到文件尾
 * return -1 读取文件出错，存储块读取偏移量回到上一个偏移处
 * return 1 正常读取，遇到文件尾，存储块读取偏移量回到上一个偏移处
 */
extern int read_data(struct store_chunk *chunk,struct buffer *buff,int unit_begin);

#endif
